<template>
  <div class="recommend">
    <div class="recommend-content" ref="scrollList">
      <div>
        <div v-if="showSwiper" class="slider-wrapper">
          <slider>
            <div v-for="item in recommends" :key="item.id">
              <a :href="item.linkUrl">
                <img :src="item.picUrl">
              </a>
            </div>
          </slider>
        </div>
        <div class="recommend-list">
          <h1 class="list-title">热门歌单推荐</h1>
          <ul>
              <li v-for="(item, index) of singList" class="item" :key="index">
                <div class="icon">
                  <img width="60" height="60" v-lazy="item.imgurl" />
                </div>
                <div class="text">
                  <h2 class="name" v-html="item.creator.name"></h2>
                  <p class="desc" v-html="item.dissname"></p>
                  <div></div>
                </div>
              </li>
            </ul>
        </div>
      </div>
      <div class="loading-container" v-show="!showSinger">
          <loading></loading>
      </div>
    </div>
  </div>
</template>

<script>
import loading from '@/base/loading/loading'
import { getList, getmusic, ERR_OK } from '@/api/recommend'
import Bscroll from 'better-scroll'
import slider from '@/base/slider/slider'
export default {
  name: 'Recommend',
  components: {
    slider,
    loading
  },
  data () {
    return {
      recommends: [],
      singList: []
    }
  },
  // keep-alive缓存数据，使用better-scroll路由切换，会出先数据不加载的问题，导致better-scroll失效。解决方法是用activated钩子函数代替create、
  activated () {
    this._getDiscList()

    this._getMusic()
  },
  mounted () {
    setTimeout(() => {
      if (!this.scroll) {
        this.scroll = new Bscroll(this.$refs.scrollList, {
          probeType: 1,
          scrollX: true,
          mouseWheel: true,
          click: true,
          taps: true,
          bounce: true
        })
      } else {
        this.scroll.refresh()
      }
    }, 20)
  },
  methods: {
    _getDiscList () {
      getList().then((res) => {
        if (res.code === ERR_OK) {
          this.recommends = res.data.slider
          // console.log(res.data.slider)
        }
      })
    },
    _getMusic () {
      getmusic().then((res) => {
        if (res.code === ERR_OK) {
          this.singList = res.data.list
        }
      })
    }
  },
  computed: {
    showSwiper () {
      return this.recommends.length
    },
    showSinger () {
      return this.singList.length
    }
  }
}
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  @import "~@/common/stylus/variable"
  .wrapper >>> .swiper-pagination-bullet
    background #fff !important
  .wrapper >>> .swiper-pagination-bullet-active
    width 24px
    border-radius 10px
  .recommend
    position: fixed
    width: 100%
    top: 88px
    bottom: 0
    .recommend-content
      height: 100%
      overflow: hidden
      .slider-wrapper
        position: relative
        width: 100%
        overflow: hidden
        .wrapper
          overflow:hidden
          width:100%
          height:0
          padding-bottom: 40%
          .swiper-img
            width:100%
      .recommend-list
        .list-title
          height: 65px
          line-height: 65px
          text-align: center
          font-size: $font-size-medium
          color: $color-theme
        .item
          display: flex
          box-sizing: border-box
          align-items: center
          padding: 0 20px 20px 20px
          .icon
            flex: 0 0 60px
            width: 60px
            padding-right: 20px
          .text
            display: flex
            flex-direction: column
            justify-content: center
            flex: 1
            line-height: 20px
            overflow: hidden
            font-size: $font-size-medium
            .name
              margin-bottom: 10px
              color: $color-text
            .desc
              color: $color-text-d
      .loading-container
        position: absolute
        width: 100%
        top: 50%
        transform: translateY(-50%)
</style>
